import { SettingPermissionService } from 'services/Api'

/* 状态相关（恢复禁用按钮）
-------------------------- */
export const StatusMixin = {
  data () {
    return {
      statusOkText: '开启',
      statusCloseText: '关闭'
    }
  },
  computed: {
    // 禁用,解禁参数
    alterAdminGroupStatusParam () {
      return {
        acc_id: this.acc_id,
        id: this.selectIds,
        menustatus: this.status
      }
    }
  },
  methods: {
    // 改变状态接口
    currentStatusApi () {
      return SettingPermissionService.ruleState(this.alterAdminGroupStatusParam)
    }
  }
}

/* 删除相关
-------------------------- */
export const DeleteMixin = {
  computed: {
    // 删除参数
    deleteParam () {
      return {
        acc_id: this.acc_id,
        id: this.selectIds
      }
    }
  },
  methods: {
    // 删除接口
    currentDeleteApi () {
      return SettingPermissionService.ruleDel(this.deleteParam)
    }
  }
}

/* 排序相关
-------------------------- */
export const SortMixin = {
  data () {
    return {
      sort: '',
      sortIndex: ''
    }
  },
  computed: {
    ruleOrderParam () {
      return {
        acc_id: this.acc_id,
        id: this.selectIds,
        sort: this.sort
      }
    }
  },
  methods: {
    async sortOne (id, index) {
      this.selectIds = id
      this.sortIndex = index
      const validator = v => !!v
      try {
        const res = await this.PromptTemp('请输入排序值', validator, '请输入排序值')
        this.confirmSort(res.value)
      } catch (e) {
        this.sort = ''
      }
    },
    confirmSort (v) {
      this.sort = v
      typeof this.sortIndex === 'number'
        ? this.AsyncLoading('排序', this.sortIndex, this.sortApi)
        : this.AsyncLoading('排序', this.sortApi)
    },
    // 接口
    async sortApi () {
      const [ err ] = await SettingPermissionService.ruleOrder(this.ruleOrderParam)
      this.sort = ''
      if (err) return this.$message.error('操作失败')
      this.$message.success(`操作成功`)
      this.getTableData()
    }
  }
}
